﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCH.Models;
using DCH.Areas.Admin.Models;
using System.Net.Mail;

namespace DCH.Controllers
{
    public class HomeController : Controller
    {
        private DbConnection db = new DbConnection();
        public ActionResult Index()
        {
           
            var featuredNews = db.New.FirstOrDefault();
            ViewBag.featuredNewsDate = featuredNews.Date.ToString("MMMM dd, yyyy");
            ViewBag.featuredNewsImage = featuredNews.imageThumbnail;
            ViewBag.featuredNewsTitle = featuredNews.Title;
            ViewBag.featuredNewsDesc = featuredNews.description;
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            ViewBag.News =  db.New.OrderByDescending(u => u.Date).Take(5);
            return View();
        }

        public ActionResult About()
        {
            
            
            
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        [HttpPost]
        public ActionResult Contact(ContatUs contatus)
        {
            if (ModelState.IsValid)
            {
               SendEmailtoAdmin(contatus);
               SendEmailtoUser(contatus);
            }
            return View();
        }

        public void SendEmailtoAdmin(ContatUs contatus)
        {
            MailMessage mailMessage = new MailMessage("info@desicricketheroes.com", "info@desicricketheroes.com");
            mailMessage.Subject = contatus.Subject;
            mailMessage.Body = contatus.Message;
            
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Send(mailMessage);
        }
        public void SendEmailtoUser(ContatUs contatus)
        {
            MailMessage mailMessage = new MailMessage("info@desicricketheroes.com", contatus.Email);
            mailMessage.Subject = "Thanks For Contacting Us";
            mailMessage.Body = "Thanks For Contacting Us. <br/> DCH Team";
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Send(mailMessage);
        }
        public ActionResult Profiles()
        {
            ViewBag.Message = "Your Profiles page.";

            return View();
        }
        public ActionResult Sponsors()
        {
           

            ViewBag.Message = "Your Sponsors page.";

            return View();
        }
        public ActionResult Register()
        {
            ViewBag.getState = new SelectList(getStates(), "Value", "Text", 1);
            ViewBag.Message = "Your Register page.";

            return View();
        }
        public ActionResult Fixture()
        {
            ViewBag.Message = "Your Register page.";

            return View();
        }
        public ActionResult Stats()
        {
            ViewBag.Message = "Your Register page.";

            return View();
        }
        public ActionResult registered()
        {
            ViewBag.Player = db.Registers.Where(reg => reg.CaptainName == null).ToList();
            return View(db.Registers.Where(reg => reg.PlayerName == null).ToList());
        }
        public ActionResult Tournament()
        {
            ViewBag.UpcomingMatch = db.Tournament.Where(d => d.Date >= DateTime.Now).ToList();
            return View(db.Tournament.Where(reg => reg.Date < DateTime.Now).ToList());

        }
        public ActionResult Terms()
        {
            return View();
        }
        public SelectListItem[] getStates()
        {
            return new[]
        {
            new SelectListItem{Value="Haryana",Text="Haryana"},
            new SelectListItem{Value="Punjab",Text="Punjab"},
        };
        }

        
    }

    public static class Util
    {
        private static DbConnection db = new DbConnection();
        public static string GetPageByName(string pagename)
        {
            var pg = (from p in db.Pages
                     where p.PageName == pagename
                     select new {  Text = p.Text 
                     
                     }).FirstOrDefault();
            return pg.Text;

        } 
    }

}
